<?php
#
#
# Copyright TEDECO, 2009. http://tedeco.fi.upm.es
#
# This file is part of kumenya.
#
# Kumenya is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Kumenya is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
#
#
#
?>
<?php
	Interfaz::CheckSession(Accountant);

	Interfaz::Record();

	$Balanceclasses = Interfaz::Data("Classes");
	$AccountsBalance = Interfaz::Data("Accounts");
	$SubaccountBalances = Interfaz::Data("Subaccount");
	

	if (!isset($Balanceclasses) && !isset($AccountsBalance) && !isset($SubaccountBalances)) {
		Interfaz::Error("You must choose at least one option");
		$result = false;
	}
	else {		
		// HERE ARE SAVED THE BALANCE'S RESULTS
		$accountsBalance = array();
		$subAccountsBalance = array();
		$classBalance = array ();
		
		// HERE ARE SAVED THE DETAILS OF EACH ACCOUNT OR CLASS
		$classDetails = array();
		$accountsDetails = array();
		$subaccountsDetails = array();
	
		$classes = AccountingAccount::GetTrialBalanceClass();

		foreach ($classes as $class) {
			if (isset($AccountsBalance) || isset($SubaccountBalances)) {
				$accounts = AccountingAccount::GetTrialBalanceAccount($class);

				foreach ($accounts as $account) {
					$criterias = array_merge (Interfaz::Data(), $account);

					// ACCOUNT DETAILS
					$accountsBalance[$class[Uid]][$account[Uid]] = AccountingAccount::SumasSaldosCuenta($criterias);
					$accountsDetails[$account[Uid]] = AccountingAccount::SeeDetails($criterias[ThirdType], $criterias);

					if ($SubaccountBalances) {
						$subaccounts = AccountingAccount::ObtenerSubcuentasSumasSaldos($account);
						foreach ($subaccounts as $subaccount) {
							$criterias = array_merge (Interfaz::Data(), $subaccount);
							// SUBACCOUNT DETAILS
							$subAccountsBalance[$account[Uid]][$subaccount[Uid]] = AccountingAccount::TrialBalanceSubaccount($criterias);
							$subaccountsDetails[$subaccount[Uid]] = AccountingAccount::SeeDetails($criterias[ThirdType], $criterias);
						}
					}
				}
			}
			$criterias = array_merge (Interfaz::Data(), $class);
			$classBalance[$class[Uid]] = AccountingAccount::SumasSaldosClase($criterias);
			$classDetails[$class[Uid]] = array (KeyNumber => $class[KeyNumber], KeyName => $class[KeyName]);

		}
		
		$header = $classBalance[$classes[0][Uid]];
		$headerDetails = $classDetails[$classes[0][Uid]];

		include "TrialBalanceBalanceExcelResult.php";
		$result = TRUE;

	}
?>